//
//  vdTextNode.m
//  Void Dead
//
//  Created by Sidney Just on 19.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdLabel.h"


@implementation vdLabel
@synthesize string;

- (void)draw:(NSTimeInterval)deltaTime {
	if(tex)
	{
		[tex drawAtPoint:CGPointMake(aPos_x + (size_x/2), mother.backingHeight - aPos_y - (size_y/2)) withColor:color];
	}
}

- (void)setString:(NSString *)aStr font:(NSString *)aFont size:(int)aSize {
	if(tex)
		[tex releaseWithTex];
	
	if(string)
		[string release];
	
	string = [[NSString alloc] initWithString:aStr];
	tex = [[Texture2D alloc] textureWithString:aStr fontName:aFont fontSize:aSize];
	
	size_x = tex.contentSize.width;
	size_y = tex.contentSize.height;
	
	if(!lastFont)
	{
		lastFont = [[NSString alloc] initWithString:aFont];
	} else {
		[lastFont release];
		lastFont = [[NSString alloc] initWithString:aFont];
	}
	
	fontSize = aSize;
}

- (void)setString:(NSString *)str {
	if(lastFont)
	{
		[self setString:str font:lastFont size:fontSize];
	}
	else {
		[self setString:str font:@"American Typewriter" size:12];
	}
	
}

+ (vdLabel *)labelWithText:(NSString *)str {
	vdLabel *temp = [[vdLabel alloc] init];
	if(str)
		[temp setString:str];
	
	return temp;
}

- (id)init {
	if(self = [super init])
	{
		texture = NULL;
		string = NULL;
		
		lastFont = NULL;
		
		size_x = 0;
		size_y = 0;
		
		color->red = 1.0f;
		color->green = 1.0f;
		color->blue = 1.0f;
		color->alpha = 1.0f;
		
		nodeShader = [[vdShaderLib sharedLib] getShader:vdShaderFixed];
	}
	return self;
}

- (void)dealloc {
	if(tex)
		[tex releaseWithTex];
	
	if(string)
		[string release];
	
	[super dealloc];
}

@end
